package com.example.smartretaildemo.module.classification.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.example.smartretaildemo.MainActivity;
import com.example.smartretaildemo.bean.classification.CommodityTypeIdInfo;
import com.example.smartretaildemo.module.classification.model.CalType;
import com.example.smartretaildemo.databinding.ActivityClaChooseBinding;
import com.example.smartretaildemo.module.classification.adapter.ChooseRViewAdapter;
import com.example.smartretaildemo.module.classification.viewmodel.ClaViewModel;
import com.example.smartretaildemo.network.OkHttpUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import okhttp3.Call;

//筛选分类界面
public class ClaChooseActivity extends AppCompatActivity {
    ActivityClaChooseBinding claChooseBinding;
//    分类名字列表
    private List<CalType> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        无标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        claChooseBinding = ActivityClaChooseBinding.inflate(getLayoutInflater());
        setContentView(claChooseBinding.getRoot());
//        异步取得所有商品类型
        OkHttpUtils.builder()
                .url(ClaViewModel.COMMODITYTYPEGETALL)
                .addParam("userId", "ODky")
                .get()
                .async(new OkHttpUtils.ICallBack() {
                    @Override
                    public void onSuccessful(Call call, String data) {
                        Gson gson = new Gson();
                        CommodityTypeIdInfo typeId = gson.fromJson(data, CommodityTypeIdInfo.class);
                        List<CommodityTypeIdInfo.DataBean> temp = typeId.getData();
                        for (int i = 0; i < temp.size(); i++) {
                            String commodityTypeName = temp.get(i).getCommodityTypeName();
                            list.add(new CalType(commodityTypeName));
                        }
//                      UI控件更新
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                claChooseBinding.rvClaCho.setLayoutManager(new LinearLayoutManager(ClaChooseActivity.this));
                                claChooseBinding.rvClaCho.setAdapter(new ChooseRViewAdapter(list, ClaChooseActivity.this));
                            }
                        });
                    }

                    @Override
                    public void onFailure(Call call, String errorMsg) {

                    }
                });
//        将所选分类存入缓存
        SharedPreferences sp = getSharedPreferences("calsList01", Context.MODE_PRIVATE);
        claChooseBinding.imgClaCho.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<Integer, Boolean> checkStatus = ChooseRViewAdapter.getCheck();
                Set<String> claList = new HashSet<>();
                for (Map.Entry<Integer, Boolean> entry : checkStatus.entrySet()) {
                    if (entry.getValue()) {
                        claList.add(String.valueOf(entry.getKey() + 1));
                    }
                }
//                至少选择1种分类
                if (claList.size() != 0) {
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putStringSet("cals", claList);
                    editor.commit();
                    Intent intent = new Intent(ClaChooseActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(ClaChooseActivity.this, "至少选择1种", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}